<?php
/**
 * Description of addImpl
 * 接收添加页面发送过来的表单，执行添加操作
 * 
 * @author ddj
 */
include '../global.php';
include LIB_PATH.'bean/Awards.php';
include LIB_PATH.'bean/ConferenceThesis.php';
include LIB_PATH.'bean/JournalThesis.php';
include LIB_PATH.'bean/Patent.php';
include LIB_PATH.'bean/Project.php';
include LIB_PATH.'bean/SoftwareCopyright.php';
include LIB_PATH.'bean/Treaties.php';
include LIB_PATH.'biz/AttributesName.php';
include LIB_PATH.'biz/add/AddInformation.php';
include LIB_PATH.'biz/add/AddAwardsInfo.php';
include LIB_PATH.'biz/add/AddConferenceThesisInfo.php';
include LIB_PATH.'biz/add/AddJournalThesisInfo.php';
include LIB_PATH.'biz/add/AddPatentInfo.php';
include LIB_PATH.'biz/add/AddProjectInfo.php';
include LIB_PATH.'biz/add/AddSoftwareCopyrightInfo.php';
include LIB_PATH.'biz/add/AddTreatiesInfo.php';
include LIB_PATH.'utils/functions.php';
include LIB_PATH.'db/db.php';


session_start();

/**
 * 执行添加命令，结果会返回到添加页面。
 * 在session中用addResult标识添加成功或者失败。
 * @param type $addInfo
 */
function doAdd($addInfo){
    $returnUrl = "../../public_files/add/add".$_GET['action'].".php";
    $headerStr = "location: ".$returnUrl;
    if($addInfo->add($GLOBALS['DB'])){
        $_SESSION['addResult']=true;
        header($headerStr);
    }else{
        $_SESSION['addResult']=false;
        header($headerStr);
    }
}

/* 判断添加哪个类别 */
$array = null;
$addInfo = null;
switch ($_GET['action']) {
    case 'Awards':
        $array = getPostValues(AttributesName::$awards);
        $awards = new Awards($array);
        $addInfo = new AddAwardsInfo($awards);
        break;
    
    case 'ConferenceThesis':
        $_POST['auditingState']=$_POST['auditingState']=="未审核" ? 0:1;
        $array = getPostValues(AttributesName::$conferenceThesis);
        $conferenceThesis = new ConferenceThesis($array);
        $addInfo = new AddConferenceThesisInfo($conferenceThesis);
        break;
    
    case 'JournalThesis':
        $_POST['auditingState']=$_POST['auditingState']=="未审核" ? 0:1;
        $array = getPostValues(AttributesName::$journalThesis);
        $journalThesis = new JournalThesis($array);
        $addInfo = new AddJournalThesisInfo($journalThesis);
        break;
    
    case 'Patent':
        $_POST['auditingState']=$_POST['auditingState']=="未审核" ? 0:1;
        $array = getPostValues(AttributesName::$patent);
        $patent = new Patent($array);
        $addInfo = new AddPatentInfo($patent);
        break;
    
    case 'Project':
        $_POST['auditingState']=$_POST['auditingState']=="未审核" ? 0:1;
        $array = getPostValues(AttributesName::$project);
        $project = new Project($array);
        $addInfo = new AddProjectInfo($project);
        break;
    
    case 'SoftwareCopyright':
        $_POST['auditingState']=$_POST['auditingState']=="未审核" ? 0:1;
        $array = getPostValues(AttributesName::$softwareCopyright);
        $softwareCopyright = new SoftwareCopyright($array);
        $addInfo = new AddSoftwareCopyrightInfo($softwareCopyright);
        break;
    
    case 'Treaties':
        $_POST['auditingState']=$_POST['auditingState']=="未审核" ? 0:1;
        $array = getPostValues(AttributesName::$treaties);
        $treaties = new Treaties($array);
        $addInfo = new addTreatiesInfo($treaties);
        break;

    default:
        header("location: ../../public_files/errorPage/401.php");
        break;
}
/* 向session中添加用户输入信息，保证添加失败后用户不用重新输入所有信息 */
$_SESSION[$_GET['action']]=$array;
doAdd($addInfo);
